package com.baseproject;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.baseproject.eventbus.EventBus;
import com.baseproject.helper.JumpCallbackHelper;
import com.baseproject.util.PreferencesUtils;
import com.baseproject.util.ToastUtils;
import com.baseproject.widget.FrameAnimatorProgressDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {

    protected JumpCallbackHelper mJumpCallbackHelper = new JumpCallbackHelper();
    public void setPurpose(String purpose) {
        mJumpCallbackHelper.setPurpose(purpose);
    }
    public String getPurpose() {
        return mJumpCallbackHelper.getPurpose();
    }


    private View mNightView;
    protected Unbinder unbinder;
    protected boolean disableNightMode = false;
    public static final String SPRFS_KEY_IS_NIGHT_MODE = "isNightMode";

    /**
     * 传递的数据不只包括下一个页面所需参数，还包含重新回到本页面时用来区分同一purpose中不同信息的参数
     */
    protected Intent intentBeforeStartActivityForResult;

    //布局文件ID
    protected abstract int getContentViewId();

    protected abstract void _onCreate(@Nullable Bundle savedInstanceState);


    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        intentBeforeStartActivityForResult = intent;
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindowFeature();
        setContentView(getContentViewId());
        unbinder = ButterKnife.bind(this);
        _onCreate(savedInstanceState);
    }


    public void setWindowFeature() {

    }

    /**
     * 夜间模式
     */
    public void night() {
        WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        lp.gravity = Gravity.BOTTOM;// 可以自定义显示的位置
        lp.y = 10;
        if (null == mNightView) {
            mNightView = new TextView(this);
            mNightView.setBackgroundColor(0x80000000);
        }
        try {
            mWindowManager.addView(mNightView, lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 普通模式
     */
    public void day() {
        if (null != mNightView) {
            WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            try {
                mWindowManager.removeView(mNightView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void startActivity(Class cls) {
        startActivity(new Intent(this, cls));
    }

    public void startActivityForResult(Class cls, int requestCode) {
        startActivityForResult(new Intent(this, cls), requestCode);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshNightMode();
    }

    protected void refreshNightMode() {
        if (!disableNightMode) {
            boolean isNightMode = PreferencesUtils.getBoolean(this, SPRFS_KEY_IS_NIGHT_MODE, false);
            if (isNightMode) {
                night();
            } else {
                day();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        FrameAnimatorProgressDialog._dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        day();
        unbinder.unbind();
        EventBus.getDefault().unRegister(this);
    }

    // 退出时间
    long currentBackPressedTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (this.getClass().equals(BaseProjectAppCofig.getInstance().getMainActivityClass())) { //如果是主页面
                int BACK_PRESSED_INTERVAL = 2000;// 退出间隔
                if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
                    currentBackPressedTime = System.currentTimeMillis();
                    ToastUtils.showToast(this.getString(R.string.leave_notification));
                } else {
                    exitThisApp();
                }
            } else {
                finish();
            }
            return true;
        }
        return false;
    }

    public void exitThisApp() {
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);
        overridePendingTransition(0, 0);
    }

}
